﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TBlog.Helpers;
using TBlog.Models;
using TBlog.Models.ViewData;

namespace TBlog.Controllers
{
    public class TBlogController : Controller
    {
        private ITBlogService _service;

        public TBlogController()
        {
            _service = new TBlogService();
        }

        //
        // GET: /TBlog/

        public ActionResult Index(int? page)
        {
            const int pageSize = 1;

            //var a = _service.ListPost();
            //var b = new PaginatedList<Post>(a,1,3); //PaginatedList<Post>();
            var _res = _service.ListCategory();

            var _model = new PostModel
            {
                Categorys = _service.ListCategory(),
                Posts = new PaginatedList<Post>(_service.ListPost(), page ?? 0, pageSize)
            };

            //return View("Post", _model);
            return RedirectToAction("Post");            
        }

        public ActionResult AddPost()
        {
            string res = "";
            for (int i = 0; i <= 3; i++)
            {
                Post postToCreate = new Post
                {
                    post_title = "my title " + i,
                    post_author = 1,
                    post_content = "my content " + i,
                    post_date = DateTime.Now,
                    post_modified = DateTime.Now,
                    post_status = "Open",
                    post_type = "Post",
                    menu_order = 0,
                    post_comment_status = "Open",
                    post_category_id = 9
                };

                if (_service.CreatePost(postToCreate))
                    res = "Create Complete.";
                else
                    res = "Create Fail.";
            }
            return Content(res);
        }

        public ActionResult AddCategory()
        {
            string res = "";
            List<Category> myCategory = new List<Category>();

            myCategory.Add(new Category() { category_name="Tavral", category_description="My Favorite Travel." });
            myCategory.Add(new Category() { category_name = "Tavral", category_description = "My Favorite Travel." });
            myCategory.Add(new Category() { category_name = "Tavral", category_description = "My Favorite Travel." });
            myCategory.Add(new Category() { category_name = "Tavral", category_description = "My Favorite Travel." });

            foreach (var mc in myCategory)
            {
                Category categoryToCreate = new Category
                {
                    category_name = mc.category_name,
                    category_description = mc.category_description
                };

                if (_service.CreateCategory(categoryToCreate))
                    res = res + "<br/>Create..... " + mc.category_name + " OK";
                else
                    res = res + "<br/>Create..... " + mc.category_name + " Fail.";
            }
            return Content(res);
        }

        //
        // Post
        public ActionResult Post(int? postId, int? page)
        {
            const int pageSize = 1;
            var post = _service.GetPost(postId);
            if (post == null)
            {
                var _model_post = new List<Post>(){ _service.GetFirstPost() };

                var _model = new PostModel
                {
                    Categorys = _service.ListCategory(),
                    Posts = new PaginatedList<Post>(_model_post, page ?? 0, pageSize)
                };
                return View("Post", _model);
                //return View("NotFound", _model);
                //return RedirectToAction("Index","TBlog");
            }
            else
            {
                //List<Post> _post = new List<Models.Post>();
                //_post.Add(post);

                var _model = new PostModel
                {
                    Categorys = _service.ListCategory(),
                    Posts = new PaginatedList<Post>(_service.ListPost(), page ?? 0, pageSize)
                };
                return View("Post", _model);
            }
        }

        //
        // Category
        public ActionResult Category(int id, int? page)
        {
            //if (id == 0) id = 1;
            //return Content("i am category : " + id + " & " + page);

            var pageSize = 1;
            var _model = new PostModel
            {
                Categorys = _service.ListCategory(),
                Posts = new PaginatedList<Post>(_service.ListPost(id).ToList(), page ?? 0, pageSize)
            };
            //return View("NotFound", _model);
            return View(_model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Comment(){
            return View();
        }

        public ActionResult About()
        {
            var page = 0;
            var pageSize = 1;
            var _model = new PostModel
            {
                Categorys = _service.ListCategory(),
                Posts = new PaginatedList<Post>(_service.ListPost().ToList(), page , pageSize)
            };
            
            return View(_model);
        }
    }
}
